/*
   思路：argc >= 3
   读argv[1] --->"r"
   写argv[2]如果文件不存在则创建 ---> "w"
 */
#include <stdio.h>
#include <string.h>

#define BUFFSIZE 32
int mycopy (FILE *rfp, FILE *wfp);

int main (int argc, char *argv[]) {
	FILE *fp1, *fp2; 
	char buf[BUFFSIZE] = {0};

	if (argc < 3) {
		return -1;
	}
	fp1 = fopen (argv[1], "r");
	if (NULL == fp1) {
		fprintf (stderr, "fopen() failed!\n");
		return -1;
	}
	fp2 = fopen (argv[2], "w");
	if (NULL == fp2) {
		fprintf (stderr, "fopen() failed!\n");
		return -1;
	}
	mycopy (fp1, fp2);
	printf ("copy后的文件位置：%ld\n", ftell(fp1));
	
	//设置文件流到文件的开头位置
	rewind (fp1);
	printf ("rewind 后：%ld\n", ftell(fp1));
	
	//定位文件流
	fseek (fp1, 5L, SEEK_SET);
	fgets (buf, BUFFSIZE, fp1);
	puts (buf);

	fclose (fp1);
	fclose (fp2);
	return 0;
}

int mycopy (FILE *rfp, FILE *wfp)
{
	char buf[BUFFSIZE] = {0};
	while (1) {
		memset (buf, 0, BUFFSIZE);
		if (NULL == fgets (buf, BUFFSIZE, rfp)) {
			if (ferror (rfp))
			{
				return -1;
			}
			break;
		}
		fputs (buf, wfp);
	}
	return 0;
}




